<template>
  <div class="ranking">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>成绩排行榜</span>
          <div class="header-actions">
            <el-select v-model="selectedExam" placeholder="选择考试" @change="loadRanking">
              <el-option
                v-for="exam in exams"
                :key="exam.id"
                :label="exam.name"
                :value="exam.id"
              />
            </el-select>
            <el-select v-model="selectedSubject" placeholder="选择科目" @change="loadRanking">
              <el-option
                v-for="subject in subjects"
                :key="subject"
                :label="subject"
                :value="subject"
              />
            </el-select>
            <el-select v-model="selectedClass" placeholder="选择班级" @change="loadRanking">
              <el-option
                v-for="classItem in classes"
                :key="classItem"
                :label="classItem"
                :value="classItem"
              />
            </el-select>
          </div>
        </div>
      </template>

      <div v-if="rankingData.length > 0" class="ranking-content">
        <el-table :data="rankingData" style="width: 100%" stripe>
          <el-table-column type="index" label="排名" width="80" />
          <el-table-column prop="studentId" label="学号" width="120" />
          <el-table-column prop="name" label="姓名" width="120" />
          <el-table-column prop="class" label="班级" width="120" />
          <el-table-column prop="score" label="分数" width="100" />
          <el-table-column prop="average" label="平均分" width="100" />
          <el-table-column label="趋势">
            <template #default="{ row }">
              <el-tag :type="getTrendType(row.trend)">
                {{ getTrendText(row.trend) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="进步名次" width="100">
            <template #default="{ row }">
              <span :class="getProgressClass(row.progress)">
                {{ row.progress > 0 ? '+' + row.progress : row.progress }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="viewDetails(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :total="total"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, sizes, prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>

      <el-empty v-else description="暂无数据" />
    </el-card>

    <!-- 学生详情对话框 -->
    <el-dialog
      v-model="studentDialogVisible"
      :title="selectedStudent?.name + '的考试详情'"
      width="60%"
    >
      <div v-if="selectedStudent" class="student-details">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="学号">{{ selectedStudent.studentId }}</el-descriptions-item>
          <el-descriptions-item label="姓名">{{ selectedStudent.name }}</el-descriptions-item>
          <el-descriptions-item label="班级">{{ selectedStudent.class }}</el-descriptions-item>
          <el-descriptions-item label="当前排名">{{ selectedStudent.rank }}</el-descriptions-item>
          <el-descriptions-item label="分数">{{ selectedStudent.score }}</el-descriptions-item>
          <el-descriptions-item label="平均分">{{ selectedStudent.average }}</el-descriptions-item>
        </el-descriptions>

        <div class="score-history">
          <h4>历史成绩</h4>
          <el-table :data="selectedStudent.scoreHistory" style="width: 100%">
            <el-table-column prop="examName" label="考试名称" />
            <el-table-column prop="score" label="分数" />
            <el-table-column prop="rank" label="排名" />
            <el-table-column prop="average" label="平均分" />
          </el-table>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

interface RankingItem {
  studentId: string
  name: string
  class: string
  score: number
  rank: number
  average: number
  trend: 'up' | 'down' | 'stable'
  progress: number
  scoreHistory: ScoreHistory[]
}

interface ScoreHistory {
  examName: string
  score: number
  rank: number
  average: number
}

const exams = ref<{ id: string; name: string }[]>([])
const subjects = ref<string[]>([])
const classes = ref<string[]>([])
const selectedExam = ref('')
const selectedSubject = ref('')
const selectedClass = ref('')
const rankingData = ref<RankingItem[]>([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const studentDialogVisible = ref(false)
const selectedStudent = ref<RankingItem | null>(null)

onMounted(() => {
  // 模拟数据，实际应从API获取
  exams.value = [
    { id: '1', name: '期中考试' },
    { id: '2', name: '期末考试' }
  ]
  subjects.value = ['数学', '英语', '语文', '物理', '化学']
  classes.value = ['高三(1)班', '高三(2)班', '高三(3)班']
  loadRanking()
})

const loadRanking = () => {
  // 模拟数据，实际应从API获取
  rankingData.value = [
    {
      studentId: '2024001',
      name: '张三',
      class: '高三(1)班',
      score: 95,
      rank: 1,
      average: 85,
      trend: 'up',
      progress: 3,
      scoreHistory: [
        {
          examName: '第一次月考',
          score: 88,
          rank: 4,
          average: 80
        },
        {
          examName: '期中考试',
          score: 92,
          rank: 2,
          average: 82
        }
      ]
    },
    {
      studentId: '2024002',
      name: '李四',
      class: '高三(1)班',
      score: 92,
      rank: 2,
      average: 85,
      trend: 'stable',
      progress: 0,
      scoreHistory: [
        {
          examName: '第一次月考',
          score: 90,
          rank: 2,
          average: 80
        },
        {
          examName: '期中考试',
          score: 92,
          rank: 2,
          average: 82
        }
      ]
    },
    {
      studentId: '2024003',
      name: '王五',
      class: '高三(2)班',
      score: 88,
      rank: 3,
      average: 85,
      trend: 'down',
      progress: -2,
      scoreHistory: [
        {
          examName: '第一次月考',
          score: 92,
          rank: 1,
          average: 80
        },
        {
          examName: '期中考试',
          score: 90,
          rank: 1,
          average: 82
        }
      ]
    }
  ]
  total.value = rankingData.value.length
}

const getTrendType = (trend: string) => {
  switch (trend) {
    case 'up':
      return 'success'
    case 'down':
      return 'danger'
    default:
      return 'info'
  }
}

const getTrendText = (trend: string) => {
  switch (trend) {
    case 'up':
      return '上升'
    case 'down':
      return '下降'
    default:
      return '稳定'
  }
}

const getProgressClass = (progress: number) => {
  if (progress > 0) return 'progress-up'
  if (progress < 0) return 'progress-down'
  return 'progress-stable'
}

const handleSizeChange = (val: number) => {
  pageSize.value = val
  loadRanking()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  loadRanking()
}

const viewDetails = (student: RankingItem) => {
  selectedStudent.value = student
  studentDialogVisible.value = true
}
</script>

<style scoped>
.ranking {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.ranking-content {
  margin-top: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.student-details {
  padding: 20px 0;
}

.score-history {
  margin-top: 20px;
}

.score-history h4 {
  margin-bottom: 15px;
  color: #333;
}

.progress-up {
  color: #67C23A;
}

.progress-down {
  color: #F56C6C;
}

.progress-stable {
  color: #909399;
}
</style> 